package top.mcwebsite.playandroid.main.ui

import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Apps
import androidx.compose.material.icons.filled.Article
import androidx.compose.material.icons.filled.Person
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.currentBackStackEntryAsState
import top.mcwebsite.playandroid.common.constant.RouteConfig
import top.mcwebsite.playandroid.resources.R as R
import top.mcwebsite.playandroid.string.R as S

sealed class PlayAndroidMainScreen(val route: String) {
    data object ArticlesScreen : PlayAndroidMainScreen(RouteConfig.Home.PATH)
    data object QuestionAnswerScreen : PlayAndroidMainScreen(RouteConfig.QA.PATH)
    data object TixiScreen : PlayAndroidMainScreen(RouteConfig.TiXi.PATH)
    data object MeScreen : PlayAndroidMainScreen(RouteConfig.Me.PATH)
}

internal val showBottomBarRoutes = buildList {
    add(PlayAndroidMainScreen.ArticlesScreen.route)
    add(PlayAndroidMainScreen.QuestionAnswerScreen.route)
    add(PlayAndroidMainScreen.TixiScreen.route)
    add(PlayAndroidMainScreen.MeScreen.route)
}

@Composable
fun BottomNavigationBar(
    playAndroidMainScreenState: MutableState<PlayAndroidMainScreen>,
    navController: NavController,
    onItemClick: (PlayAndroidMainScreen) -> Unit
) {
    val navBackStackEntry by navController.currentBackStackEntryAsState()
    val currentRoute = navBackStackEntry?.destination?.route ?: "articles"
    if (showBottomBarRoutes.contains(currentRoute)) {
        NavigationBar {
            NavigationBarItem(
                selected = playAndroidMainScreenState.value is PlayAndroidMainScreen.ArticlesScreen,
                onClick = {
                    onItemClick(PlayAndroidMainScreen.ArticlesScreen)
                },
                icon = { Icon(Icons.Default.Article, contentDescription = null) },
                label = { Text(text = stringResource(id = S.string.article)) }
            )
            NavigationBarItem(
                selected = playAndroidMainScreenState.value is PlayAndroidMainScreen.QuestionAnswerScreen,
                onClick = { onItemClick(PlayAndroidMainScreen.QuestionAnswerScreen) },
                icon = {
                    Icon(
                        bitmap = ImageBitmap.imageResource(id = R.drawable.quest_and_answer),
                        contentDescription = null,
                        modifier = Modifier.size(24.dp)
                    )
                },
                label = { Text(stringResource(id = S.string.question_answer)) },
            )
            NavigationBarItem(
                selected = playAndroidMainScreenState.value is PlayAndroidMainScreen.TixiScreen,
                onClick = { onItemClick(PlayAndroidMainScreen.TixiScreen) },
                icon = {
                    Icon(Icons.Default.Apps, contentDescription = null)
                },
                label = { Text(stringResource(S.string.tixi)) },
            )
            NavigationBarItem(
                selected = playAndroidMainScreenState.value is PlayAndroidMainScreen.MeScreen,
                onClick = { onItemClick(PlayAndroidMainScreen.MeScreen) },
                icon = {
                    Icon(Icons.Default.Person, contentDescription = null)
                },
                label = { Text(stringResource(id = S.string.me)) }
            )
        }
    }
}